package board;

import java.net.*;
import java.util.*;

import javax.servlet.http.*;

import model.*;
import community.*;

import org.apache.struts.action.*;
import org.apache.struts.actions.*;

import util.*;

public class NoticeBoardAction extends DispatchAction {

	/**
	 * 리스트 호출 메소드
	 * */
	public ActionForward getList(ActionMapping mapping, ActionForm form, 
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		NoticeForm nf = (NoticeForm)form;
		User user = (User)request.getSession().getAttribute("user");
		
		int cono = 0;
				
		if(!nf.getCono().equals("")||nf.getCono()!=null) {
			cono = Integer.parseInt(request.getParameter("cono"));
			nf.setCono(cono+"");
		} else {
			cono = Integer.parseInt(nf.getCono());
		}
//		메뉴 세팅 //
		if(cono<10) {
			Menu menu = new Menu();
			menu = FreeBoardAction.setMainMenu(menu);
			request.getSession().setAttribute("menu", menu);			
		} else {
			CommunityGo.setMemberCommunity(request);
		}
				
		Pager pager = new Pager();
		
		NoticeBoardDAO nbDAO = new NoticeBoardDAO();
		long totalRows = nbDAO.getTotalRows(cono);
		pager.init(request, totalRows, 10, 5);
		
		Vector<NoticeBoard> list = nbDAO.getBoardListPage(cono, pager.getCurrentPage(), pager.getRowsPerPage());
				
		ArrayList<NoticeForm> noticelist = new ArrayList<NoticeForm>();
		
		for(NoticeBoard nboard : list){
			noticelist.add(this.castDTOToForm(new NoticeForm(),nboard));
		}
		nf.setCono(cono+"");
		nf.setNoticelist(noticelist);
		
		request.getSession().setAttribute("pager", pager);
		
		return mapping.findForward("noticelist");
	}


	/**
	 * 메뉴생성메소드
	 * */
	public ActionForward setMenu(ActionMapping mapping, ActionForm form, 
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		NoticeForm nf = (NoticeForm)form;
		User user = (User)request.getSession().getAttribute("user");
		
		Menu menu = new Menu();
		String cono = request.getParameter("cono");
		
		BoardNames bns = new BoardNames();
		bns.setBname("공지사항");
		String url = "noticeboard.do?method=getList&cono="+cono+"&nbname=";
		bns.setUrl(url);
		Vector<BoardNames> v = new Vector<BoardNames>();
		v.add(bns);
		menu.setNoticeBoardMenus(v);
		request.getSession().setAttribute("menu", menu);
		
		return getList(mapping,form,request,response	);
	}
	
	public static Vector<BoardNames> setMainNoticeMenu(){
		
		BoardNames bns1 = new BoardNames();
		bns1.setBname("공지사항");
		String url1 = "noticeboard.do?method=getList&cono=6&nbname=";
		bns1.setUrl(url1);
		
		BoardNames bns2 = new BoardNames();
		bns2.setBname("ITEA 뉴스");
		String url = "noticeboard.do?method=getList&cono=8&nbname=";
		bns2.setUrl(url);
		
		Vector<BoardNames> v = new Vector<BoardNames>();
		v.add(bns1);
		v.add(bns2);
		return v;
	}
	
	/**
	 * 게시물 뷰 메소드
	 * */
	public ActionForward detailView(ActionMapping mapping, ActionForm form, 
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		NoticeForm nf = (NoticeForm)form;
		User user = (User)request.getSession().getAttribute("user");
		int nbno = Integer.parseInt(request.getParameter("nbno"));
		nf.setMid(user.getMid());
		if(request.getParameter("cono")!=null&&request.getParameter("cono")!="") {
			if(Integer.parseInt(request.getParameter("cono"))<10){
				CommunityGo.setMemberCommunity(request);				
			}
		}
		NoticeBoardDAO nbDAO =new NoticeBoardDAO();
		NoticeBoard nb = nbDAO.getNoticeBoard(nbno);
		
		nf = this.castDTOToForm(nf, nb);

		int next = nbDAO.getNext(nb);
		int preview = nbDAO.getPreview(nb);
		
		String rownum = nbDAO.getRownum(nb.getCono(), nbno);
		nf.setRownum(rownum);		
		
		if(next>nbno) nf.setNext(this.castDTOToForm(new NoticeForm(), nbDAO.getNoticeBoard(next)));
		else nf.setNext(null);
		if(preview<nbno) nf.setPreview(this.castDTOToForm(new NoticeForm(), nbDAO.getNoticeBoard(preview)));
		else nf.setPreview(null);
		
		if(!user.getMid().equals(nf.getMid())){
			nbDAO.setCounter(nbno);	//increase counter
		}
		
		
		return mapping.findForward("noticeview");
	}
	
	/**
	 * 게시물 작성 메소드
	 * */
	public ActionForward getForm(ActionMapping mapping, ActionForm form, 
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		NoticeForm nf = (NoticeForm)form;
		User user = (User)request.getSession().getAttribute("user");
		
		nf.setMid(user.getMid());
		nf.setCono(request.getParameter("cono"));
		nf.setMcnick(MemberCommunityDAO.getCommunityNick(user.getMid(), Long.parseLong(nf.getCono())));
				
		if(nf.getNbtitle()==null||nf.getNbtitle().equals("")){
			return mapping.findForward("noticeinsert");	
		} else {
			return mapping.findForward("noticeupdate");
		}
		
	}
	
	public ActionForward insert(ActionMapping mapping, ActionForm form, 
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		NoticeForm nf = (NoticeForm)form;
		User user = (User)request.getSession().getAttribute("user");
		String cono = request.getParameter("cono");
		
		NoticeBoardDAO nbDAO =new NoticeBoardDAO();
		nf.setMid(user.getMid());
		nf.setCono(cono);
		nf.setNbno("0");
		nf.setNbcounter("0");
		
		
		if(nbDAO.insert(this.castFormToDTO(nf))>0){
			return getList(mapping, form, request, response);
		}		
		
		return mapping.findForward("noticeinsert");
	}	

	/**
	 * 게시물 수정 메소드
	 * */
	public ActionForward update(ActionMapping mapping, ActionForm form, 
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		NoticeForm nf = (NoticeForm)form;
		User user = (User)request.getSession().getAttribute("user");
		NoticeBoardDAO nbDAO = new NoticeBoardDAO();
		if(nbDAO.update(this.castFormToDTO(nf))>0){
			return getList(mapping, form, request, response);
		}		
		return mapping.findForward("noticeupdate");
	}
	
	/**
	 * 게시물 삭제 메소드
	 * */
	public ActionForward delete(ActionMapping mapping, ActionForm form, 
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		NoticeForm nf = (NoticeForm)form;
		User user = (User)request.getSession().getAttribute("user");
		NoticeBoardDAO nbDAO = new NoticeBoardDAO();
		
		if(nbDAO.delete(this.castFormToDTO(nf))>0){
			return getList(mapping, form, request, response);
		}		
		return mapping.findForward("noticeview");
	}
	
	/**
	 * ActionForm to DTO 캐스팅 메소드
	 * */	
	public static NoticeBoard castFormToDTO(NoticeForm nf){
		NoticeBoard nb = new NoticeBoard(); 
		
		nb.setNbno(Integer.parseInt(nf.getNbno()));
		nb.setCono(Integer.parseInt(nf.getCono()));
		nb.setMid(nf.getMid());
		nb.setNbtitle(nf.getNbtitle());
//		nb.setNbdate(nf.getNbdate());
		nb.setNbcontent(nf.getNbcontent());
//		nb.setNbcounter(Integer.parseInt(nf.getNbcounter()));
				
		return nb;
	}
	
	/**
	 * DTO to ActionForm 캐스팅 메소드
	 * */
	public static NoticeForm castDTOToForm(NoticeForm nf,NoticeBoard nb){
		
		nf.setNbno(nb.getNbno()+"");
		nf.setCono(nb.getCono()+"");
		nf.setMid(nb.getMid());
		nf.setNbtitle(nb.getNbtitle());
		nf.setNbdate(nb.getNbdate().substring(0,19));
		nf.setNbsdate(nb.getNbdate().substring(0,10));
		nf.setNbcontent(nb.getNbcontent());
		nf.setNbcounter(nb.getNbcounter()+"");
		nf.setRownum(nb.getRownum()+"");
		
		String mcnick = MemberCommunityDAO.getCommunityNick(nb.getMid(), nb.getCono());
		nf.setMcnick(mcnick!=null?mcnick:nb.getMid());
		
		return nf;
	}

}
